package com.lei.retrofitlibrary.http.product;

import com.lei.retrofitlibrary.http.url.BaseUrl;
import com.lei.retrofitlibrary.http.token.TokenInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @ProjectName : YoHuo001Java
 * @Author : 雷金泷
 * @Time : 2020/12/11 15:46
 * @Description : 文件描述
 */
public class NetWorkProduct implements IRetrofitProduct {
    private Retrofit retrofit;

    @Override
    public void create() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BaseUrl.BASE_URL)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .client(new OkHttpClient.Builder()
                        .readTimeout(50, TimeUnit.SECONDS)
                        .writeTimeout(50, TimeUnit.SECONDS)
                        .connectTimeout(50, TimeUnit.SECONDS)
                        .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                        .addInterceptor(new TokenInterceptor())
                        .build())
                .build();
    }

    @Override
    public Retrofit getRetrofit() {
        if (retrofit==null){
            create();
        }
        return retrofit;
    }
}
